package org.dromara.erp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.domain.AuditDto;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.erp.domain.ErpFarmSale;
import org.dromara.erp.service.IErpFarmSaleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 农场销售
 *
 * @author Mr Lei QQ:235921049
 * @date 2024-06-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/farmSale")
public class ErpFarmSaleController extends BaseController {

    private final IErpFarmSaleService erpFarmSaleService;

    /**
     * 查询农场销售列表
     */
    @SaCheckPermission("erp:farmSale:list")
    @GetMapping("/list")
    public TableDataInfo<ErpFarmSale> list(ErpFarmSale bo, PageQuery pageQuery) {
        return erpFarmSaleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出农场销售列表
     */
    @SaCheckPermission("erp:farmSale:export")
    @Log(title = "农场销售", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpFarmSale bo, HttpServletResponse response) {
        List<ErpFarmSale> list = erpFarmSaleService.queryList(bo);
        ExcelUtil.exportExcel(list, "农场销售", ErpFarmSale.class, response);
    }

    /**
     * 获取农场销售详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erp:farmSale:query")
    @GetMapping("/{id}")
    public R<ErpFarmSale> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(erpFarmSaleService.queryById(id));
    }

    /**
     * 新增农场销售
     */
    @SaCheckPermission("erp:farmSale:add")
    @Log(title = "农场销售", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpFarmSale bo) {
        return toAjax(erpFarmSaleService.insertByBo(bo));
    }

    /**
     * 修改农场销售
     */
    @SaCheckPermission("erp:farmSale:edit")
    @Log(title = "农场销售", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpFarmSale bo) {
        return toAjax(erpFarmSaleService.updateByBo(bo));
    }

    /**
     * 删除农场销售
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erp:farmSale:remove")
    @Log(title = "农场销售", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(erpFarmSaleService.deleteWithValidByIds(List.of(ids), true));
    }
    /*生成默认标题*/
    @GetMapping("/title")
    public R<String> defaultTitle() {
        //根据统计当日列表数量，生成默认标题
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd");//日期格式化
        LocalDate today = LocalDate.now();//今天
        String formatDate = today.format(formatter);
        //生成标题
        String defTitle = erpFarmSaleService.defaultTitle(formatDate);
        return R.ok("ok", "XS" + formatDate + defTitle);
    }
    /**
     * 审核操作
     */
    @SaCheckPermission("chicken:reduce:audit")
    @PutMapping("/audit")
    public R<Void> updateAudit(@RequestBody AuditDto dto) {
        erpFarmSaleService.updateAudit(dto);
        return R.ok();
    }
}
